home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / WINER.ZIP / MOUSE.BAS < prev    next >
BASIC Source File  |  1992-05-13  |  10KB  |  416 lines

  1. '********** MOUSE.BAS - demonstrates accessing the various mouse services
  2.  
  3. 'Copyright (c) 1992 Ethan Winer
  4.  
  5. DEFINT A-Z
  6.  
  7. '---- assembly language functions and subroutines
  8. DECLARE FUNCTION PeekWord% (BYVAL Segment, BYVAL Address)
  9. DECLARE SUB MouseInt (MouseRegs AS ANY)
  10.  
  11.  
  12. '---- BASIC functions and subprograms
  13. DECLARE FUNCTION Bin2Hex% (Binary$)
  14. DECLARE FUNCTION MouseThere% ()
  15. DECLARE FUNCTION WaitButton% ()
  16. DECLARE SUB CursorShape (HotX, HotY, Shape())
  17. DECLARE SUB HideCursor ()
  18. DECLARE SUB MouseTrap (ULRow, ULCol, LRRow, LRCol)
  19. DECLARE SUB MoveCursor (X, Y)
  20. DECLARE SUB ReadCursor (X, Y, Buttons)
  21. DECLARE SUB ShowCursor ()
  22. DECLARE SUB TextCursor (FG, BG)
  23.  
  24. DECLARE SUB Prompt (Message$)   'used for this demo only
  25.  
  26.  
  27. TYPE MouseType                  'similar to DOS Registers
  28.   AX      AS INTEGER
  29.   BX      AS INTEGER
  30.   CX      AS INTEGER
  31.   DX      AS INTEGER
  32.   Segment AS INTEGER
  33. END TYPE
  34.  
  35. DIM SHARED MouseRegs AS MouseType 'so all the subs can get at them
  36. DIM SHARED MousePresent
  37. REDIM Cursor(1 TO 32)           'holds the cursor shape definition
  38.  
  39. IF NOT MouseThere% THEN         'ensure that a mouse is present
  40.   PRINT "No mouse is installed" '  and initialize it if so
  41.   END
  42. END IF
  43. CLS
  44.  
  45.  
  46. DEF SEG = 0                     'see what type of monitor
  47. IF PEEK(&H463) <> &HB4 THEN     'if it's color
  48.   ColorMon = -1                 'remember that for later
  49.   SCREEN 12                     'this requires a VGA
  50.   LINE (0, 0)-(639, 460), 1, BF 'paint a blue background
  51. END IF
  52.  
  53.  
  54. DIM Choice$(1 TO 5)             'display some choices on the screen,
  55. LOCATE 1, 1                     '  so we'll have something to point at
  56. FOR X = 1 TO 5
  57.   READ Choice$(X)
  58.   PRINT Choice$(X);
  59.   LOCATE , X * 12
  60. NEXT
  61. DATA "Choice 1", "Choice 2", "Choice 3", "Choice 4", "Choice 5"
  62.  
  63.  
  64. IF NOT ColorMon THEN            'if it's not color
  65.   CALL TextCursor(-2, -2)       'select a text cursor
  66. END IF
  67.  
  68.  
  69. CALL ShowCursor
  70. CALL Prompt("Point the cursor at a choice, and press a button.")
  71.  
  72.  
  73. DO                              'wait for a button press
  74.   CALL ReadCursor(X, Y, Button)
  75. LOOP UNTIL Button
  76. IF Button AND 4 THEN Button = 3 'for three-button mice
  77.  
  78. CALL Prompt("You pressed button" + STR$(Button) + " and the cursor was at location" + STR$(X) + "," + STR$(Y) + " - press a button.")
  79.  
  80. IF ColorMon THEN                'if it is a color monitor
  81.   RESTORE Arrow                 '  load a custom arrow
  82.   GOSUB DefineCursor
  83. END IF
  84. Dummy = WaitButton%
  85.  
  86.  
  87.  
  88. IF ColorMon THEN                'the hardware can do it
  89.   RESTORE CrossHairs            'set a cross-hairs cursor
  90.   GOSUB DefineCursor
  91.   CALL Prompt("Now the cursor is a cross-hairs, press a button.")
  92.   Dummy% = WaitButton%
  93. END IF
  94.  
  95.  
  96.  
  97. IF ColorMon THEN                'now set an hour glass
  98.   RESTORE HourGlass
  99.   GOSUB DefineCursor
  100. END IF
  101.  
  102.  
  103. CALL Prompt("Now notice how the cursor range is restricted.  Press a button to end.")
  104. CALL MouseTrap(50, 50, 100, 100)
  105. Dummy = WaitButton%
  106.  
  107. IF ColorMon THEN                'restore to 640 x 350
  108.   CALL MouseTrap(0, 0, 349, 639)
  109. ELSE                            'use CGA coordinates for mono!
  110.   CALL MouseTrap(0, 0, 199, 639)
  111. END IF
  112.  
  113. Dummy = InitMouse%              'reset the mouse driver
  114. CALL HideCursor                 'and turn off the cursor
  115. SCREEN 0                        'revert to text mode
  116. END
  117.  
  118.  
  119.  
  120. DefineCursor:
  121.  
  122. FOR X = 1 TO 32                 'read 32 words of data
  123.   READ Dat$                     'read the data
  124.   Cursor(X) = Bin2Hex%(Dat$)    'convert to integer
  125. NEXT
  126. CALL CursorShape(Zero, Zero, Cursor())
  127. RETURN
  128.  
  129.  
  130.  
  131. Arrow:
  132.  
  133. NOTES:
  134. 'The first group of binary data is the screen mask.
  135. 'The second group of binary data is the cursor mask.
  136. 'The cursor color is black where both masks are 0.
  137. 'The cursor color is XORed where both masks are 1.
  138. 'The color is clear where the screen mask is 1 and the cursor mask is 0.
  139. 'The color is white where the screen mask is 0 and the cursor mask is 1.
  140. '
  141. '--- this is the screen mask
  142. DATA "1110011111111111"
  143. DATA "1110001111111111"
  144. DATA "1110000111111111"
  145. DATA "1110000011111111"
  146. DATA "1110000001111111"
  147. DATA "1110000000111111"
  148. DATA "1110000000011111"
  149. DATA "1110000000001111"
  150. DATA "1110000000000111"
  151. DATA "1110000000000011"
  152. DATA "1110000000000001"
  153. DATA "1110000000011111"
  154. DATA "1110001000011111"
  155. DATA "1111111100001111"
  156. DATA "1111111100001111"
  157. DATA "1111111110001111"
  158.  
  159. '---- this is the cursor mask
  160. DATA "0001100000000000"
  161. DATA "0001010000000000"
  162. DATA "0001001000000000"
  163. DATA "0001000100000000"
  164. DATA "0001000010000000"
  165. DATA "0001000001000000"
  166. DATA "0001000000100000"
  167. DATA "0001000000010000"
  168. DATA "0001000000001000"
  169. DATA "0001000000000100"
  170. DATA "0001000000111110"
  171. DATA "0001001100100000"
  172. DATA "0001110100100000"
  173. DATA "0000000010010000"
  174. DATA "0000000010010000"
  175. DATA "0000000001110000"
  176.  
  177.  
  178.  
  179. CrossHairs:
  180.  
  181. DATA "1111111101111111"
  182. DATA "1111111101111111"
  183. DATA "1111111101111111"
  184. DATA "1111000000000111"
  185. DATA "1111011101110111"
  186. DATA "1111011101110111"
  187. DATA "1111011111110111"
  188. DATA "1000000111000000"
  189. DATA "1111011111110111"
  190. DATA "1111011101110111"
  191. DATA "1111011101110111"
  192. DATA "1111000000000111"
  193. DATA "1111111101111111"
  194. DATA "1111111101111111"
  195. DATA "1111111101111111"
  196. DATA "1111111111111111"
  197.  
  198. DATA "0000000010000000"
  199. DATA "0000000010000000"
  200. DATA "0000000010000000"
  201. DATA "0000111111111000"
  202. DATA "0000100010001000"
  203. DATA "0000100010001000"
  204. DATA "0000100000001000"
  205. DATA "0111111000111111"
  206. DATA "0000100000001000"
  207. DATA "0000100010001000"
  208. DATA "0000100010001000"
  209. DATA "0000111111111000"
  210. DATA "0000000010000000"
  211. DATA "0000000010000000"
  212. DATA "0000000010000000"
  213. DATA "0000000000000000"
  214.  
  215.  
  216.  
  217. HourGlass:
  218.  
  219. DATA "1100000000000111"
  220. DATA "1100000000000111"
  221. DATA "1100000000000111"
  222. DATA "1110000000001111"
  223. DATA "1110000000001111"
  224. DATA "1111000000011111"
  225. DATA "1111100000111111"
  226. DATA "1111110001111111"
  227. DATA "1111110001111111"
  228. DATA "1111100000111111"
  229. DATA "1111000000011111"
  230. DATA "1110000000001111"
  231. DATA "1110000000001111"
  232. DATA "1100000000000111"
  233. DATA "1100000000000111"
  234. DATA "1100000000000111"
  235.  
  236. DATA "0000000000000000"
  237. DATA "0001111111110000"
  238. DATA "0000000000000000"
  239. DATA "0000111111100000"
  240. DATA "0000100110100000"
  241. DATA "0000010001000000"
  242. DATA "0000001010000000"
  243. DATA "0000000100000000"
  244. DATA "0000000100000000"
  245. DATA "0000001010000000"
  246. DATA "0000011111000000"
  247. DATA "0000110001100000"
  248. DATA "0000100000100000"
  249. DATA "0000000000000000"
  250. DATA "0001111111110000"
  251. DATA "0000000000000000"
  252.  
  253. FUNCTION Bin2Hex% (Binary$) STATIC  'binary to integer
  254.  
  255.   Temp& = 0
  256.   Count = 0
  257.  
  258.   FOR X = LEN(Binary$) TO 1 STEP -1
  259.     IF MID$(Binary$, X, 1) = "1" THEN
  260.       Temp& = Temp& + 2 ^ Count
  261.     END IF
  262.     Count = Count + 1
  263.   NEXT
  264.  
  265.   IF Temp& > 32767 THEN Temp& = Temp& - 65536
  266.   Bin2Hex% = Temp&
  267.  
  268. END FUNCTION
  269.  
  270. SUB CursorShape (HotX, HotY, Shape()) STATIC
  271.  
  272.   IF NOT MousePresent THEN EXIT SUB
  273.  
  274.   MouseRegs.AX = 9
  275.   MouseRegs.BX = HotX
  276.   MouseRegs.CX = HotY
  277.   MouseRegs.DX = VARPTR(Shape(1))
  278.   MouseRegs.Segment = VARSEG(Shape(1))
  279.  
  280.   CALL MouseInt(MouseRegs)
  281.  
  282. END SUB
  283.  
  284. SUB HideCursor STATIC       'turns off the mouse cursor
  285.  
  286.   IF NOT MousePresent THEN EXIT SUB
  287.  
  288.   MouseRegs.AX = 2
  289.   CALL MouseInt(MouseRegs)
  290.  
  291. END SUB
  292.  
  293. FUNCTION MouseThere% STATIC     'reports if a mouse is present
  294.  
  295.   MouseThere% = 0               'assume there is no mouse
  296.   IF PeekWord%(Zero, (4 * &H33) + 2) = 0 THEN 'if segment = 0
  297.     EXIT FUNCTION                             '  then there's no mouse
  298.   END IF
  299.  
  300.   MouseRegs.AX = 0
  301.   CALL MouseInt(MouseRegs)
  302.   MouseThere% = MouseRegs.AX
  303.   IF MouseRegs.AX THEN MousePresent = -1
  304.  
  305. END FUNCTION
  306.  
  307. SUB MouseTrap (ULRow, ULColumn, LRRow, LRColumn) STATIC
  308.  
  309.   IF NOT MousePresent THEN EXIT SUB
  310.  
  311.   MouseRegs.AX = 7      'restrict horizontal movement
  312.   MouseRegs.CX = ULColumn
  313.   MouseRegs.DX = LRColumn
  314.   CALL MouseInt(MouseRegs)
  315.  
  316.   MouseRegs.AX = 8      'restrict vertical movement
  317.   MouseRegs.CX = ULRow
  318.   MouseRegs.DX = LRRow
  319.   CALL MouseInt(MouseRegs)
  320.  
  321. END SUB
  322.  
  323. SUB MoveCursor (X, Y) STATIC    'positions the mouse cursor
  324.  
  325.   IF NOT MousePresent THEN EXIT SUB
  326.  
  327.   MouseRegs.AX = 4
  328.   MouseRegs.CX = X
  329.   MouseRegs.DX = Y
  330.   CALL MouseInt(MouseRegs)
  331.  
  332. END SUB
  333.  
  334. SUB Prompt (Message$) STATIC    'prints prompt message
  335.  
  336.     V = CSRLIN                  'save current cursor position
  337.     H = POS(0)
  338.     LOCATE 30, 1                'use 25 for SCREEN 9
  339.     CALL HideCursor             'this is very important!
  340.     PRINT LEFT$(Message$, 79); TAB(80);
  341.     CALL ShowCursor             'and so is this
  342.     LOCATE V, H                 'restore the cursor
  343.  
  344. END SUB
  345.  
  346. SUB ReadCursor (X, Y, Buttons)  'returns cursor and button information
  347.  
  348.   IF NOT MousePresent THEN EXIT SUB
  349.  
  350.   MouseRegs.AX = 3
  351.   CALL MouseInt(MouseRegs)
  352.  
  353.   Buttons = MouseRegs.BX AND 7
  354.   X = MouseRegs.CX
  355.   Y = MouseRegs.DX
  356.  
  357. END SUB
  358.  
  359. SUB ShowCursor STATIC           'turns on the mouse cursor
  360.  
  361.   IF NOT MousePresent THEN EXIT SUB
  362.  
  363.   MouseRegs.AX = 1
  364.   CALL MouseInt(MouseRegs)
  365.  
  366. END SUB
  367.  
  368. SUB TextCursor (FG, BG) STATIC
  369.  
  370.   IF NOT MousePresent THEN EXIT SUB
  371.  
  372.   MouseRegs.AX = 10
  373.   MouseRegs.BX = 0
  374.   MouseRegs.CX = &HFF
  375.   MouseRegs.DX = 0
  376.  
  377.   IF FG = -1 THEN                       'maintain FG as the cursor moves?
  378.     MouseRegs.CX = MouseRegs.CX OR &HF00
  379.   ELSEIF FG = -2 THEN                   'invert FG as the cursor moves?
  380.     MouseRegs.CX = MouseRegs.CX OR &H700
  381.     MouseRegs.DX = &H700
  382.   ELSE
  383.     MouseRegs.DX = 256 * (FG AND &HFF)  'use the specified color
  384.   END IF
  385.  
  386.   IF BG = -1 THEN                       'maintain BG as the cursor moves?
  387.     MouseRegs.CX = MouseRegs.CX OR &HF000
  388.   ELSEIF BG = -2 THEN                   'invert BG as the cursor moves?
  389.     MouseRegs.CX = MouseRegs.CX OR &H7000
  390.     MouseRegs.DX = MouseRegs.DX OR &H7000
  391.   ELSE
  392.     Temp = (BG AND 7) * 16 * 256
  393.     MouseRegs.DX = MouseRegs.DX OR Temp 'use the specified color
  394.   END IF
  395.  
  396.   CALL MouseInt(MouseRegs)
  397.  
  398. END SUB
  399.  
  400. FUNCTION WaitButton% STATIC     'waits for a button press
  401.  
  402.   IF NOT MousePresent THEN EXIT FUNCTION
  403.  
  404.   X! = TIMER                    'pause to allow releasing
  405.   WHILE X! + .2 > TIMER         '  the button
  406.   WEND
  407.  
  408.   DO                            'wait for a button press
  409.     CALL ReadCursor(X, Y, Button)
  410.   LOOP UNTIL Button
  411.  
  412.   IF Button AND 4 THEN Button = 3 'for three-button mice
  413.   WaitButton% = Button            'assign the function
  414.  
  415. END FUNCTION
  416.